home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2024 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  51 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: atol or strtol ?
  5. Date: 18 Jan 1996 15:34:14 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan18103414@g7240065.bridge.bst.bls.com>
  8. References: <4dbobq$763@jupiter.planet.net> <4dk4sc$dmj@spanky.pls.ov.com>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: glenn@ov.com's message of 18 Jan 1996 00:39:40 GMT
  11.  
  12. In article <4dk4sc$dmj@spanky.pls.ov.com> glenn@ov.com (Fletcher.Glenn@ov.com) writes:
  13.  
  14. : In article 763@jupiter.planet.net, Chris Kemp <chrisk@paladn.com> writes:
  15. : >what is the difference between  strtol function and atol ?
  16.  
  17. : Looking at TFM tells me that atol() expects the string to represent a
  18. : base 10 number.  strtol() allows you to select the expected number base.
  19.  
  20. Also strtol will set it's second parameter (if supplied) to point to the charater
  21. that terminate the scan. This allows you to continue processing the buffer.
  22.  
  23. For example:
  24.  
  25.     char* p;
  26.     char* ptr;
  27.     ...
  28.     fgets(buf, sizeof(buf), stdin);
  29.     ptr = buf;
  30.  
  31.     for (;;) {
  32.         num = strtol(ptr, &p, 0);
  33.         if (errno && errno != ERANGE)    /* We can deal with ERANGE problems. */
  34.         deal with error.
  35.  
  36.         if (p == ptr) {
  37.             if (end of buf)
  38.                 break;
  39.             deal with bad formed input
  40.         } else
  41.             ptr = p;
  42.  
  43.         do something with num
  44.     }
  45.  
  46. Regards
  47.  
  48.     -A.
  49. -- 
  50. | A.Champion                |
  51.